atspicontext: Fix GetIndexInParent for toplevels
authorMatthias Clasen <mclasen@redhat.com>
Sat, 10 Oct 2020 16:22:16 +0000 (12:22 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Mon, 12 Oct 2020 19:10:40 +0000 (15:10 -0400)
For toplevels, we need to return the index in the
list of toplevels, since that is what GtkAtspiRoot
is using.

gtk/a11y/gtkatspicontext.c

index b7031a9a92659512d3553d6f8df41e121804bbd6..d932f06c2c8409caa4a3ad816443fc7c9a705df1 100644 (file)
@@ -32,6 +32,8 @@
 
 #include "gtkdebug.h"
 #include "gtkwindow.h"
+#include "gtklabel.h"
+#include "gtkroot.h"
 
 #include <gio/gio.h>
 
@@ -224,6 +226,57 @@ collect_relations (GtkAtSpiContext *self,
     }
 }
 
+static int
+get_index_in_parent (GtkWidget *widget)
+{
+  GtkWidget *parent = gtk_widget_get_parent (widget);
+  GtkWidget *child;
+  int idx;
+
+  idx = 0;
+  for (child = gtk_widget_get_first_child (parent);
+       child;
+       child = gtk_widget_get_next_sibling (child))
+    {
+      if (!gtk_widget_get_visible (child))
+        continue;
+
+      if (child == widget)
+        break;
+
+      idx++;
+    }
+
+  return idx;
+}
+
+static int
+get_index_in_toplevels (GtkWidget *widget)
+{
+  GListModel *toplevels = gtk_window_get_toplevels ();
+  guint n_toplevels = g_list_model_get_n_items (toplevels);
+  GtkWidget *window;
+  int idx;
+
+  idx = 0;
+  for (guint i = 0; i < n_toplevels; i++)
+    {
+      window = g_list_model_get_item (toplevels, i);
+
+      g_object_unref (window);
+
+      if (!gtk_widget_get_visible (window))
+        continue;
+
+      if (window == widget)
+        break;
+
+      idx += 1;
+    }
+
+  return idx;
+}
+
 static void
 handle_accessible_method (GDBusConnection       *connection,
                           const gchar           *sender,
@@ -347,24 +400,12 @@ handle_accessible_method (GDBusConnection       *connection,
   else if (g_strcmp0 (method_name, "GetIndexInParent") == 0)
     {
       GtkAccessible *accessible = gtk_at_context_get_accessible (GTK_AT_CONTEXT (self));
-      GtkWidget *widget = GTK_WIDGET (accessible);
-      GtkWidget *parent = gtk_widget_get_parent (widget);
-      GtkWidget *child;
       int idx;
 
-      idx = 0;
-      for (child = gtk_widget_get_first_child (parent);
-           child;
-           child = gtk_widget_get_next_sibling (child))
-        {
-          if (!gtk_widget_get_visible (child))
-            continue;
-
-          if (child == widget)
-            break;
-
-          idx++;
-        }
+      if (GTK_IS_ROOT (accessible))
+        idx = get_index_in_toplevels (GTK_WIDGET (accessible));
+      else
+        idx = get_index_in_parent (GTK_WIDGET (accessible));
 
       g_dbus_method_invocation_return_value (invocation, g_variant_new ("(i)", idx));
     }